<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/5/7
 * Time: 15:00
 */

namespace app\admin\controller;

use app\admin\model\GoldOrder as OrderModel;
use think\Db;

class Goldorder extends Base
{
    private $model;
    public function _initialize()
    {
        $this->model = new OrderModel();
    }
//分类显示
    public function index(){

        if (request()->isAjax()) {
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            //查询未处理的订单
            $where['status'] = '0';
            $selectResult = $this->model->getMenuByWhere($where, $offset, $limit);
            //查询订单中的业务id（bid)
            $arrbid = Db::name('gold_order')->field('bid')->where('status',0)->select();
            $newarr = [];
            foreach($arrbid as $k=>$v){
                $newarr[] = $v['bid'];
            }
            //查询对应的menuname
            $res = Db::name('goldOrder')->alias('o')
                    ->join('__MENU_CONTENT__ m','o.bid=m.id')
                    ->field('m.menuname')
                    ->where('m.id','in',$newarr)
                    ->select();

            $status = config('status');
            //拼接参数
            foreach ($selectResult as $k => $v) {
                $selectResult[$k]['status'] = $status[$v['status']];
                $selectResult[$k]['menuname'] = $res[$k]['menuname'];
                $selectResult[$k]['operate'] = showOperate($this->makeButton($v['id']));
            }
            $result['total'] = $this->model->getAllCount($where);//总数据
            $result['rows'] = $selectResult;

            return json($result);
        }
        return $this->fetch();
    }
    //已处理订单列表
    public function indexprocess(){

        if (request()->isAjax()) {
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            //查询未处理的订单
            $where['status'] = '1';
            $selectResult = $this->model->getMenuByWhere($where, $offset, $limit);
            $arrbid = Db::name('gold_order')->field('bid')->where('status',1)->select();

            $newarr = [];
            foreach($arrbid as $k=>$v){
                $newarr[] = $v['bid'];
            }
            $res = Db::name('GoldOrder')->alias('o')
                ->join('__MENU_CONTENT__ m','o.bid=m.id')
                ->field('m.menuname')
                ->where('m.id','in',$newarr)
                ->select();

            $status = config('status');
            //拼接参数
            foreach ($selectResult as $k => $v) {
                $selectResult[$k]['status'] = $status[$v['status']];
                $selectResult[$k]['menuname'] = $res[$k]['menuname'];
                $selectResult[$k]['operate'] = showOperate($this->makeButtonp($v['id']));
            }
            $result['total'] = $this->model->getAllCount($where);//总数据
            $result['rows'] = $selectResult;
            return json($result);
        }
        return $this->fetch();
    }
    //查看分类订单
    public function show(){
        $id = input('param.id');
        $res = $this->model->getOneOrder($id);
        $order = OrderModel::get($id);
        $result = $order->menucontent;
        $userinfo = Db::name('gold_order')->alias('o')
                    ->join('__USER__ u','o.uid=u.id')
                    ->where('o.id',$id)
                    ->find();


        $res['menuname'] = $result['menuname'];
        $res['username'] = $userinfo['username'];

        $this->assign(['data'=>$res,'status'=>config('status')]);
        return $this->fetch();
    }

    public function process(){
        $id = input('param.id');
        $res = $this->model->getOneOrder($id);
        $order = OrderModel::get($id);
        $result = $order->menucontent;
        $userinfo = Db::name('gold_order')->alias('o')
            ->join('__USER__ u','o.uid=u.id')
            ->where('o.id',$id)
            ->find();


        $res['menuname'] = $result['menuname'];
        $res['username'] = $userinfo['username'];

        $this->assign(['data'=>$res,'status'=>config('status')]);
        return $this->fetch();
    }
    public function add()
    {
        if(request()->isPost()){
            $param = input('post.');
            $flag = $this->model->addMenu($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $this->assign('status',config('code.status_normal'));
        return $this->fetch();
    }

    public function edit()
    {
        if(request()->isPost()){
            $param = input('post.');


            $flag = $this->model->editOrder($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $id = input('param.id');
        $data = $this->model->getOneMenu($id);

        $this->assign([
            'data' => $data,
            'status'=>config('code.status_normal')
        ]);
        return $this->fetch();
    }

    public function editp()
    {
        if(request()->isPost()){
            $param = input('post.');


            $flag = $this->model->editOrderp($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $id = input('param.id');
        $data = $this->model->getOneMenu($id);

        $this->assign([
            'data' => $data,
            'status'=>config('code.status_normal')
        ]);
        return $this->fetch();
    }

    public function uploadImg()
    {
        if(request()->isAjax()){

            $file = request()->file('file');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload/gift');
            if($info){
                $src =  '/upload/gift' . '/' . date('Ymd') . '/' . $info->getFilename();
                return json(msg(0, ['src' => $src], ''));
            }else{
                // 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }

    public function del(){
        if ($this->request->isGet()) {
            $id = input('param.id');
            $flag = $this->model->saveStatus($id);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '查看' => [
                'auth' => 'Goldorder/showlist',
                'href' => url('Goldorder/show', ['id' => $id]),
                'btnStyle' => 'success',
                'icon' => 'fa fa-clipboard'
            ],
            '删除' => [
                'auth' => 'Goldorder/del',
                'href' => "javascript:adminDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }

    private function makeButtonp($id)
    {
        return [
            '查看' => [
                'auth' => 'Goldorder/process',
                'href' => url('Goldorder/process', ['id' => $id]),
                'btnStyle' => 'success',
                'icon' => 'fa fa-clipboard'
            ],
            '删除' => [
                'auth' => 'Goldorder/del',
                'href' => "javascript:adminDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}